$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'goodsPush/goodspush/list',
        datatype: "json",
        colModel: [			
			{ label: 'goodsId', name: 'goodsId', index: 'goods_id', width: 50, key: true ,align: 'center'},
			{ label: '商品名称', name: 'name', index: 'name', width: 80 ,align: 'center'},
			{ label: '权重', name: 'pushWeight', index: 'push_weight', width: 80 ,align: 'center'},
			{ label: '应用到搜索', name: 'searchFlag', index: 'search_flag', width: 80 ,align: 'center',
                formatter:function(value, options, row){
                    return value?'是':'否'
                }},
			{ label: '应用到分类', name: 'categoryFlag', index: 'category_flag', width: 80 ,align: 'center',
                formatter:function(value, options, row){
                    return value?'是':'否'
                }},
			{ label: '是否删除', name: 'delFlag', index: 'del_flag', width: 80 ,align: 'center',
                formatter:function(value, options, row){
                    return value==1?'是':'否'
                }},
			{ label: '创建时间', name: 'createDate', index: 'create_date', width: 80 ,align: 'center'},
			{ label: '更新时间', name: 'updateDate', index: 'update_date', width: 80 ,align: 'center'},
			{ label: '创建人员', name: 'createUser', index: 'create_user', width: 80 ,align: 'center'},
			{ label: '更新人员', name: 'updateUser', index: 'update_user', width: 80 ,align: 'center'}
        ],
		viewrecords: true,
        height: 700,
        rowNum: 25,
		rowList : [25,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		goodsPush: {},
		q:{
			name:''
		},
        loading: false,
        goodsList: [],
        options: [],
		tempList:[]
	},
    mounted() {
        $.get(baseURL + "goodsPush/goodspush/goodsList", function (r) {
        	vm.tempList=r.goodsList
            vm.goodsList = vm.tempList.map(item => {
                return {value: item.goods_id, label: item.goods_id+'-'+item.name};
            });
        });
    },
	methods: {
		query: function () {
			vm.reload();
		},
        reset() {
			vm.q.name=''
            vm.reload();
        },
		add: function(){
			vm.title = "新增";
			vm.goodsPush = {
                searchFlag:true,
                categoryFlag:true,
                delFlag:false
			};
            vm.showList = false;
		},
		update: function (event) {
			var goodsId = getSelectedRow();
			if(goodsId == null){
				return ;
			}
            vm.title = "修改";
            vm.getInfo(goodsId)
            vm.showList = false;
		},
		saveOrUpdate: function (event) {
			if (vm.title=='新增'&&!vm.goodsPush.name){
                alert('商品名称不能为空')
                return
			}
			if (typeof vm.goodsPush.goodsId=='undefined'){
                vm.goodsPush.goodsId=vm.goodsPush.name
			}
            // vm.goodsPush.goodsId=vm.goodsPush.goodsId==null?vm.goodsPush.name:vm.goodsPush.goodsId
            if (!vm.goodsPush.pushWeight) {
            	alert('权重不能为空')
				return
            }
			var url = vm.title == '新增' ? "goodsPush/goodspush/save" : "goodsPush/goodspush/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.goodsPush),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var goodsIds = getSelectedRows();
			if(goodsIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "goodsPush/goodspush/delete",
                    contentType: "application/json",
				    data: JSON.stringify(goodsIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(goodsId){
			$.get(baseURL + "goodsPush/goodspush/info/"+goodsId, function(r){
                vm.goodsPush = r.goodsPush;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    'name': vm.q.name.trim()
                },
                page:1
            }).trigger("reloadGrid");
		},
        remoteMethod(query) {
            if (query !== '') {
                vm.loading = true;
                setTimeout(() => {
                    vm.loading = false;
                    vm.options = vm.goodsList.filter(item => {
                        return item.label.indexOf(query) > -1;
                    });
                }, 200);
            } else {
                vm.options = [];
            }
        }
	}
});